<?php

$id = empty($this->Param['id']) ? 0 : $this->Param['id'];
$this->Assign('id', $id);

$this->LoadHelper('EstimateHelper');
$EstimateHelper = new EstimateHelper();

$this->LoadHelper('MemberHelper');
$MemberHelper = new MemberHelper();
$member_info = $MemberHelper->getMember($this->UserConfig['Id']);

$this->LoadHelper('BankHelper');
$BankHelper = new BankHelper();

$this->LoadHelper('OrderHelper');
$OrderHelper = new OrderHelper();

if (empty($this->Param['option'])) {
    $ajax = $this->Param['ajax'];
    $page = isset($this->Param['page']) ? $this->Param['page'] : 1;
    $rp = isset($this->Param['rp']) ? $this->Param['rp'] : PAGE_SIZE;
    if($ajax){
        $where = array(
            '`et_status` NOT IN (?)'=> array('作废', '已使用')
        );
        $data = $EstimateHelper->estimateList($where, $page, $rp, $this->Param,$member_info);
        $json_data = array('total' => $data['count'], 'rows' => array());
        if (!empty($data['all']))
            foreach ($data['all'] AS $rowNum => $row) {
                $row['et_stime'] = empty($row['et_stime']) ? '' : date('Y-m-d H:i:s', $row['et_stime']);
                $row['et_ftime'] = empty($row['et_ftime']) ? '' : date('Y-m-d H:i:s', $row['et_ftime']);
                $bank_name = $BankHelper->bankRow($row['et_bank']);
                $row['et_bank_name'] = $bank_name['bk_name'];
                //$row['info_url'] = '/estimate/edit/?id='.$row['et_id'];
                $json_data['rows'][] = $row;
            }
        echo json_encode($json_data);
    }
    else {
        $this->Assign('member_info', $member_info);
        echo $this->GetView('index.php');
    }
} else {
    switch ($this->Param['option']) {
        case 'etinfo':
            $where = array(
                '`bk_status` = ?' => 'ok',
                '`bk_id` IN (?)' => empty($member_info['u_bank']) ? array(0) : explode(',', $member_info['u_bank'])
            );
            $bank = $BankHelper->bankAll($where);

            $data = $EstimateHelper->estimateRow($id);
            $data['bank']=$bank;
            if($data['et_bank']){
            $bank_name=$BankHelper->bankRow($data['et_bank']);
            $data['et_bank']=$bank_name['bk_name'];
        }
            //$data['attachment'] = $EstimateHelper->attachmentOut($data['et_attach']);
            $data['attachment'] = $OrderHelper->etattachmentOut($data['et_attach']);
            $ff=preg_match_all("/(\d+(\.\d+)?)+/",$data['et_remarks'],$arr);
            $data['et_carprice']=$arr[0][0];
            $data['et_loan']=$arr[0][1];
            $data['et_rate']=$arr[0][2];
            $this->Assign('data', $data);
            echo $this->GetView('edit.php');
            break;
        case 'retinfo':
            $data = $EstimateHelper->estimateRow($id);
            //$data['attachment'] = $EstimateHelper->attachmentOut($data['et_attach']);
            $data['attachment'] = $OrderHelper->etattachmentOut($data['et_attach']);
            $ff=preg_match_all("/(\d+(\.\d+)?)+/",$data['et_remarks_two'],$arr);
            $data['et_carprice']=$arr[0][0];
            $data['et_loan']=$arr[0][1];
            $data['et_rate']=$arr[0][2];
            $data['et_rprice']=1;

            if($data['et_bank']){
                $bank_name=$BankHelper->bankRow($data['et_bank']);
                $data['et_bank']=$bank_name['bk_name'];
            }

            $this->Assign('data', $data);
            echo $this->GetView('edit.php');
            break;
        case 'save':
            $params = $this->Param;
            $edata = $EstimateHelper->estimateRow($id);
            $member_info = $MemberHelper->getMember($this->UserConfig['Id']);
            $this->Assign('member_info', $member_info);
            $arr=array('待评估','已使用','已评估');
            if(in_array($params['et_status'],$arr)){
                ErrorMsg::Debug('当前状态不能修改');

                break;
            }

            $params['et_sname'] = $member_info['u_realname'];
            $params['et_br_name'] = $member_info['u_dpname'];
            $params['et_br_id']=$member_info['u_dp_id'];
            $params['et_sid'] = $member_info['u_id'];
            $params['et_status'] ='待评估';
            $params['et_remarks']='希望车价'.$params['et_carprice'].'元，贷款额'.$params['et_loan'].'元，公司费率'.$params['et_rate'].'%。';

            if($id > 0){
                $result = $EstimateHelper->estimateSave($params, $id);
            }else{
                $result = $EstimateHelper->estimateSave($params);
            }
            if($result['success']){
                $EstimateHelper->updateAttachtemp($result['msg'],$params['temp_id']);
                $desc = $id > 0 ? '修改评估信息' : '新增评估信息';
                $MemberHelper->saveLog($this->UserConfig, $desc, json_encode($this->Param), $this->Controller.'_'.$this->Action);
                ErrorMsg::Debug('保存成功', TRUE);
            }
            ErrorMsg::Debug($result['msg']);
            break;
        case 'rsave':
            $params = $this->Param;
            $member_info = $MemberHelper->getMember($this->UserConfig['Id']);
            $edata = $EstimateHelper->estimateRow($id);
            $this->Assign('member_info', $member_info);
            $id = empty($this->Param['id']) ? ErrorMsg::Debug('参数错误') : $this->Param['id'];
            if($edata['et_price_two']>0){
                ErrorMsg::Debug('已存在重评记录');
                break;
            }
            $params['et_status'] ='待评估';
            if($edata['et_stime']>0){
                $params['et_stime_two']=time();
            }
            $result = $EstimateHelper->estimateSave($params, $id);

            if($result['success']){
                $desc ='重评信息';
                $MemberHelper->saveLog($this->UserConfig, $desc, json_encode($this->Param), $this->Controller.'_'.$this->Action);
                ErrorMsg::Debug('保存成功', TRUE);
            }
            ErrorMsg::Debug($result['msg']);
            break;
        case 'remove':
            $id = empty($this->Param['id']) ? ErrorMsg::Debug('参数错误') : $this->Param['id'];
            $result = $EstimateHelper->estimateRemove($id);
            if($result){
                $MemberHelper->saveLog($this->UserConfig, '删除评估', json_encode($this->Param), $this->Controller.'_'.$this->Action);
                ErrorMsg::Debug('删除成功');
            }
            ErrorMsg::Debug('删除失败');
            break;
        case 'updata':
            $params = $this->Param;
            $member_info = $MemberHelper->getMember($this->UserConfig['Id']);
            $this->Assign('member_info', $member_info);
            $edata = $EstimateHelper->estimateRow($id);
            $arr=array('已使用','已评估');
            if(in_array($edata['et_status'],$arr)){
                ErrorMsg::Debug('当前状态不能修改');
                break;
            }
            $params['et_sname'] = $member_info['u_realname'];
            $params['et_br_name'] = $member_info['u_dpname'];
            if($id > 0){
                $result = $EstimateHelper->estimateSave($params, $id);
            }else{
                $result = $EstimateHelper->estimateSave($params);
            }
            if($result['success']){
                $desc = $id > 0 ? '修改评估信息' : '新增评估信息';
                $MemberHelper->saveLog($this->UserConfig, $desc, json_encode($this->Param), $this->Controller.'_'.$this->Action);
                ErrorMsg::Debug('保存成功', TRUE);
            }
            ErrorMsg::Debug($result['msg']);
            break;
        default:
    }

}